<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label="车牌号" prop="carNo">
                <el-input v-model="queryParams.carNo" placeholder="请输入车牌号" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="交强险时效">
                <el-select v-model="queryParams.jqType">
                    <template v-for="item in dict.type.insurance_status">
                        <el-option :label="item.label" :value="item.value" :key="item.value"></el-option>
                    </template>
                </el-select>
            </el-form-item>
            <el-form-item label="商业险时效">
                <el-select v-model="queryParams.syType">
                    <template v-for="item in dict.type.insurance_status">
                        <el-option :label="item.label" :value="item.value" :key="item.value"></el-option>
                    </template>
                </el-select>
            </el-form-item>
            <el-form-item label="车辆状态" prop="carStatus">
                <el-select v-model="queryParams.carStatus">
                    <template v-for="item in dict.type.car_status">
                        <el-option :label="item.label" :value="item.value" :key="item.value"></el-option>
                    </template>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['maintenance:insurance:add']">新增</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['maintenance:insurance:remove']">删除</el-button>
            </el-col> -->
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['maintenance:insurance:export']">导出</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport" v-hasPermi="['system:user:import']">导入</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="insuranceList" @selection-change="handleSelectionChange">
            <el-table-column label="车牌号" align="left" prop="carNo" width="100" />
            <el-table-column label="车架号" align="center" prop="vinNo" />
            <el-table-column label="车辆信息" align="center" prop="carInfo" />
            <el-table-column label="所属门店" align="center" prop="deptName" />
            <el-table-column label="所属租户" align="center" prop="tenantName" />
            <el-table-column label="车辆状态" align="center" prop="carStatus" width="100">
                <template slot-scope="{row}">
                    <dict-tag :options="dict.type.car_status" :value="row.carStatus" />
                </template>
            </el-table-column>
            <el-table-column label="商业险信息" width="100" align="center">
                <template slot-scope="{row}">
                    <el-popover trigger="click" width="600" placement="top">
                        <el-descriptions title="商业险信息" :column="2">
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="商业险保险公司名称">
                                {{row.sycompulsoryCompany||'--'}}
                            </el-descriptions-item>
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="商业险保单号">
                                {{row.sycompulsoryNo||'--'}}
                            </el-descriptions-item>
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="商业险合计费用">
                                {{row.sycommercialTotalAmount||'--'}}
                            </el-descriptions-item>
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="商业险开始时间">
                                {{parseTime(row.sycompulsoryStartTime, '{y}-{m}-{d}')||'--'}}
                            </el-descriptions-item>
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="商业险结束时间">
                                {{parseTime(row.sycompulsoryEndTime, '{y}-{m}-{d}')||'--'}}
                            </el-descriptions-item>
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="商业险保险剩余天数" v-if="row.syDay>=0">
                                {{row.syDay||0}}
                            </el-descriptions-item>
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="商业险保险逾期天数" v-if="row.syDay<0">
                                {{Math.abs(row.syDay||0)}}
                            </el-descriptions-item>
                        </el-descriptions>
                        <el-tag slot="reference" :type="{2:'danger',3:'warning'}[row.syType]" style="width:58px;cursor: pointer;">
                            <!-- {{row.syType}} -->
                            <!-- <i class="el-icon-search" style="margin-right:5px"></i> -->
                            <dict-tag :options="dict.type.mmv_syType" :value="row.syType" />
                        </el-tag>
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column label="交强险信息" width="100" align="center">
                <template slot-scope="{row}">
                    <el-popover trigger="click" width="600" placement="top">
                        <el-descriptions title="交强险信息" :column="2">
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="交强险保险公司名称">
                                {{row.jqcompulsoryCompany||'--'}}
                            </el-descriptions-item>
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="交强险保单号">
                                {{row.jqcompulsoryNo||'--'}}
                            </el-descriptions-item>
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="交强险保险费用">
                                {{row.jqcompulsoryAmount||'--'}}
                            </el-descriptions-item>
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="交强险开始时间">
                                {{parseTime(row.jqcompulsoryStartTime, '{y}-{m}-{d}')||'--'}}
                            </el-descriptions-item>
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="交强险结束时间">
                                {{parseTime(row.jqcompulsoryEndTime, '{y}-{m}-{d}')||'--'}}
                            </el-descriptions-item>
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="交强险保险剩余天数" v-if="row.jqDay>=0">
                                {{row.jqDay||0}}
                            </el-descriptions-item>
                            <el-descriptions-item label-class-name="my-descriptions-label"  label="交强险保险逾期天数" v-if="row.jqDay<0">
                                {{Math.abs(row.jqDay||0)}}
                            </el-descriptions-item>
                        </el-descriptions>
                        <!-- <el-tag slot="reference" :type="row.jqType==2?'danger':''" style="width:58px;cursor: pointer;"> -->
                        <el-tag slot="reference" :type="{2:'danger',3:'warning'}[row.jqType]" style="width:58px;cursor: pointer;">
                            <!-- {{row.jqType}} -->
                            <!-- <i class="el-icon-search" style="margin-right:5px"></i> -->
                            <dict-tag :options="dict.type.mmv_syType" :value="row.jqType" />
                        </el-tag>
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300px" fixed="right">
                <template slot-scope="scope">
                    <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['maintenance:insurance:edit']" v-if="scope.row.syType==1 && scope.row.jqType==1">修改</el-button> -->
                    <el-button size="mini" type="text" icon="el-icon-search" @click="handleDetail(scope.row)" v-hasPermi="['maintenance:insurance:edit']">详情</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['maintenance:insurance:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="syUpdate(scope.row)" v-hasPermi="['maintenance:insurance:edit']" v-if="scope.row.syType==2 || scope.row.syType==3">商业险续保</el-button>
                    <el-button size="mini" type="text" L icon="el-icon-edit" @click="jqUpdate(scope.row)" v-hasPermi="['maintenance:insurance:edit']" v-if="scope.row.jqType==2 || scope.row.jqType==3">交强险续保</el-button>
                    <!-- <el-button size="mini"
                               type="text"
                               icon="el-icon-delete"
                               @click="handleDelete(scope.row)"
                               v-hasPermi="['maintenance:insurance:remove']">删除</el-button> -->
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改保险管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="1100px" destroy-on-close append-to-body @close="editNowId=null">
            <MmvAdd ref="MmvAdd" :carId="editNowId" v-if="open" @close="closeAddForm"></MmvAdd>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm" :loading="submitLoading">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 添加或修改保险管理对话框 -->
        <el-dialog :title="title" :visible.sync="open2" width="1100px" destroy-on-close append-to-body @close="editNowId=null">
            <MmvAdd2 ref="MmvAdd2" :carId="editNowId" v-if="open2"></MmvAdd2>
            <div slot="footer" class="dialog-footer">
                <!-- <el-button type="primary" @click="submitForm" :loading="submitLoading">确 定</el-button> -->
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <MmvSy ref="MmvSy" :carId="MmvSyCarId" :key="mmvkey" :title="title" :opensy="opensy" @close="closeSyForm"></MmvSy>
        <MmvJq ref="MmvJq" :carId="MmvJqCarId" :key="mmvkey" :title="title" :openJq="openJq" @close="closeJqForm"></MmvJq>

        <!-- 用户导入对话框 -->
        <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
            <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                <div class="el-upload__tip text-center" slot="tip">
                    <!-- <div class="el-upload__tip" slot="tip">
                        <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
                    </div> -->
                    <span>仅允许导入xls、xlsx格式文件。</span>
                    <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
                </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listInsurance,
    getInsurance,
    delInsurance,
    addInsurance,
    updateInsurance,
    exportInsurance,
    addSyInsurance,
    addJqInsurance,
    importTemplate,
	downloadTemplate,
} from "@/api/maintenance/insurance";
import MmvAdd from './add.vue';
import MmvAdd2 from './detail.vue';
import MmvSy from './syrenewal.vue';
import MmvJq from './jqrenewal.vue';
import { getToken } from "@/utils/auth";

export default {
    name: "Insurance",
    components: {
        MmvAdd,
        MmvAdd2,
        MmvSy,
        MmvJq
    },
    dicts: ['company_type', 'insurance_status', 'company_isAvailable', 'mmv_syType', 'car_status'],
    data() {
        return {
            mmvkey: 1,
            submitLoading: false,
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 保险管理表格数据
            insuranceList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            open2: false,
            opensy: false,
            MmvSyCarId: '',
            MmvJqCarId: '',
            openJq: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                carId: null,
                carNo: null,
                vinNo: null,
                carInfo: null,
                carStatus: null,
                compulsoryCompanyId: null,
                compulsoryCompany: null,
                compulsoryNo: null,
                compulsoryStartTime: null,
                compulsoryEndTime: null,
                compulsoryAmount: null,
                compulsoryRemark: null,
                commercialTotalAmount: null,
                insuranceStatus: null,
                isAvailable: null,
                deptId: null,
                jqType: null,
                syType: null,
                tenantId: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            editNow: null,
            // 用户导入参数
            upload: {
                // 是否显示弹出层（用户导入）
                open: false,
                // 弹出层标题（用户导入）
                title: "",
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的用户数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { Authorization: "Bearer " + getToken() },
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + "/maintenance/insurance/import"
            },
        };
    },
    computed: {
        editNowId() {
            return this.editNow?.carId;
        },
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询保险管理列表 */
        getList() {
            this.loading = true;
            listInsurance(this.queryParams).then(response => {
                this.insuranceList = response.rows;
                this.total = response.total;
                this.loading = false;
                this.mmvkey++
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.open2 = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                carId: null,
                carNo: null,
                vinNo: null,
                carInfo: null,
                compulsoryCompanyId: null,
                compulsoryCompany: null,
                compulsoryNo: null,
                compulsoryStartTime: null,
                compulsoryEndTime: null,
                compulsoryAmount: null,
                compulsoryRemark: null,
                commercialTotalAmount: null,
                insuranceStatus: 0,
                carStatus: null,
                jqType: null,
                syType: null,
                delFlag: null,
                isAvailable: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null,
                deptId: null,
                tenantId: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams.jqType = null
            this.queryParams.syType = null
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.carId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd(row) {
            this.reset();
            this.open = true;
            this.title = "添加保险管理";
            this.editNow = row;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.editNow = row;
            this.reset();
            const id = row.carId || this.ids
            getInsurance(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改保险管理";
            });
        },
        /** 修改按钮操作 */
        handleDetail(row) {
            this.editNow = row;
            this.reset();
            const id = row.carId || this.ids
            getInsurance(id).then(response => {
                this.form = response.data;
                this.open2 = true;
                this.title = "保险详情";
            });
        },
        syUpdate(row) {
            this.editNow = row;
            const id = row.carId || this.ids
            this.MmvSyCarId = id
            this.opensy = true;
            this.title = "商业险续保";
        },
        jqUpdate(row) {
            this.editNow = row;
            const id = row.carId || this.ids
            this.MmvJqCarId = id
            this.openJq = true;
            this.title = "交强险续保";
        },
        closeAddForm() {
            this.open = false;
            this.getList();
        },
        closeSyForm() {
            this.opensy = false;
            this.getList();
        },
        closeJqForm() {
            this.openJq = false;
            this.getList();
        },
        /** 导入按钮操作 */
        handleImport() {
            this.upload.title = "导入";
            this.upload.open = true;
        },
        /** 下载模板操作 */
        importTemplate() {
            /* importTemplate().then(response => {
                this.$download.name(response.msg);
,            }); */
			//return downloadTemplate();,
			const queryParams = this.queryParams;
			downloadTemplate(queryParams).then(response => {
                this.$download.name(response.msg);
            });
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
            this.getList();
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
        },
        /** 提交按钮 */
        async submitForm() {
            this.submitLoading = true;
            setTimeout(() => {
                this.submitLoading = false;
            }, 2e3)
            try {
                this.submitLoading = true;
                await this.$refs?.MmvAdd?.submitData();
            } catch (error) {
                this.submitLoading = false;
            } finally {
                this.submitLoading = false;
            }
        },
        //商业险续保
        async submitSyForm() {
            await this.$refs.MmvSy.submitSyForm();
        },
        //交强险续保
        async submitJqForm() {
            await this.$refs.MmvJq.submitJqForm();
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除数据项？').then(function () {
                return delInsurance(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$modal.confirm('是否确认导出所有保险管理数据项？').then(() => {
                this.exportLoading = true;
                return exportInsurance(queryParams);
            }).then(response => {
                this.$download.name(response.msg);
                this.exportLoading = false;
            }).catch(() => { });
        }
    }
};
</script>
